<?php
    $modifiedSoftware = ns_dao_software::SoftwareByID($_GET['id']);
    $categoryList = ns_dao_software::GetCategoryList();
    $userList = ns_dao_user::GetUsersList();
?>
<div id="topMenu">
<!--Corner/Side Divs-->
<div class="topMenu-Top">
<div class="topMenu-Bot">
<div class="topMenu-Left">
<div class="topMenu-Right">
<div class="topMenu-BotL">
<div class="topMenu-BotR">
<div class="topMenu-TopL">
<div class="topMenu-TopR">
<!--Corner/Side Divs-->

<table width="100%" border="0" cellpadding="1" cellspacing="1" align="center">
<tr>
    <td class="highlightCell" align="center"><a href="my_projects.php?id=<?php echo $_GET['id'] ?>&view=manageDescr"><strong>Software Description</strong></a></td>
    <td class="highlightCell" align="center"><a href="my_projects.php?id=<?php echo $_GET['id'] ?>&view=manageIcon"><strong>Software Icon</strong></a></td>
    <td class="highlightCell" align="center"><a href="my_projects.php?id=<?php echo $_GET['id'] ?>&view=manageVersion"><strong>Software File Management</strong></a></td>
</tr>
</table>

<!--Corner/Side Divs-->
</div></div></div></div></div></div></div></div>
<!--Corner/Side Divs-->
</div>
</div> <!-- End of topSection -->

<form name="softwareVersion" action="my_projects.php" method="POST">
    <input type="hidden" name="softwareID" id="softwareID" value="<?php echo $_GET['id'] ?>"/>
    <table border="0" width="500" cellspacing="1" cellpadding="1">
        <tr>
            <td colspan="4"><strong>Software Versions</strong><br />
            <?php if (!isset($_GET['go'])) { ?>
                <input type="button" value="Create New Version" onclick="location.href='manage_content.php?id=<?php echo $_GET['id']; ?>&view=manageVersion&go=newVersion'" /></td>
            <?php } ?>
        </tr>                
        <?php
            $versionList = ns_dao_software::GetSoftwareVersionListByID($_GET['id']);                    
            //==========If there is no version of this software==========
            if (isset($_GET['go']) && $_GET['go'] == "newVersion" || $versionList == false)
            {
        ?>
                <tr>
                    <td><strong>Software ID</strong></td>
                    <td><?php echo $_GET['id']; ?></td>
                </tr>
                <tr>
                    <td><strong>Version</strong></td>
                    <td><input type="text" name="newVersionNum" id="newVersionNum" /></td>
                </tr>
                <tr>
                    <td><strong>Date</strong></td>
                    <td><input type="text" name="newVersionDate" id="newVersionDate" value="<?php echo date("d-m-Y"); ?>"/></td>
                </tr>
                <!--<tr>
                    <td><strong>Directory</strong></td>
                    <td><?php echo SoftwareVersion::SoftwareStoredPath(); ?></td>
                </tr>
                <tr>
                    <td><strong>Filename</strong></td>
                    <td><input type="file" name="versionFile" id="versionFile" /></td>
                </tr>-->
                <tr>
                    <td colspan="1">&nbsp;</td>
                    <td style="" align="left" nowrap="nowrap">
                    <input type="submit" name="createVersion" id="createVersion" value="Save" style="font-weight: bold;" />
                    <input type="button" name="cancel" id="cancel" value="Cancel" onclick="window.location.href ='my_projects.php'" />
                    </td>
                </tr>
        <?php
            } else { //Just show the list of existing verions of this software                  
        ?>          
                <tr>
                    <td><strong>Version</strong></td>
                    <td><strong>Last Updated</strong></td>
                    <td><strong>File Location</strong></td>
                    <td><strong>Delete</strong></td>
                </tr>
        <?php                    
                foreach ($versionList as $key => $version)
                {
                    echo "<tr>";
                    echo "<td><a href=my_projects.php?id=".$_GET['id']."&view=manageVersion&version=".$version->SoftwareVersionID().">".$version->VersionNumber()."</a></td>";
                    echo "<td>".$version->UpdateDate()."</td>";
                    echo "<td>"."../../content/software/" . $_GET['id'] . "/" . $version->SoftwareVersionID()."</td>";

                    echo '<td><form name="deleteVersion" action="my_projects.php" method="post">';
                    echo '<input type="hidden" name="softwareVersionID" id="softwareVersionID" value="'.$version->SoftwareVersionID().'"/>';
                    echo '<input type="submit" name="deleteVersion" id="deleteVersion" value="Delete" onclick="return confirm(\'Are you sure?\');">';
                    echo "</form></td>";
                    echo "</tr>";
                }
            }
        ?>               
                
    </table>
    </form><br />
    
    <?php
        if (isset($_GET['version']))
        {
            $dirName = "../../content/software/" . $_GET['id'] . "/" . $version->SoftwareVersionID();
            $dir = opendir($dirName);
            $fileList = array();

            while (false != ($file = readdir($dir)))
            {
                if ($file != "." && $file != "..")
                {
                    array_push($fileList, '<a href="' . $dirName . '/'. $file . '">' . $file . '</a>');
                }
            }
            closedir($dir);		
            sort($fileList);
            
            if (count($fileList) == 0)
            {
                echo '<strong>There are no files associated with this version.</strong><br />';
            }
            
?>
                <form name="uploadFile" action="my_projects.php" method="POST" enctype="multipart/form-data">
                <input type="hidden" name="softwareID" id="softwareID" value="<?php echo $_GET['id']; ?>"/>
                <input type="hidden" name="versionID" id="version" value="<?php echo $version->SoftwareVersionID(); ?>"/>
                    <strong>Filename</strong>
                    <input type="file" name="versionFile" id="versionFile" />
                    <input type="submit" name="uploadFile" id="uploadFile" value="Upload" />
                </form>
<?php                
            if (count($fileList) != 0) {
                echo '<strong>Files in this version:</strong><br />';
                foreach ($fileList as $k)
                {
                    echo $k."<br />";
                }
            }
        }
    ?>